package org.ko.core.websupport.log.impl;

import javax.servlet.http.HttpServletRequest;

import org.ko.core.domain.support.LogInfo;

/** 
 * 将request放入ThreadLocal
 */

public abstract class LogUitls {
	// 用于存储每个线程的request请求
	private static final ThreadLocal<HttpServletRequest> LOCAL_REQUEST = new ThreadLocal<HttpServletRequest>();
	
	public static void putRequest(HttpServletRequest request) {
		LOCAL_REQUEST.set(request);
	}
	
	public static HttpServletRequest getRequest() {
		return LOCAL_REQUEST.get();
	}
	
	public static void removeRequest() {
		LOCAL_REQUEST.remove();
	}
	
	/**
	 * 将日志信息放到LogInfo 的remark 中
	 * @param remark
	 */
	public static void putLog(String remark){
		HttpServletRequest req = getRequest();
		if(req != null)
			req.setAttribute(LogInfo.REMARK, remark);
	}
	
	/**
	 *<pre>
	 *保存错误日志
	 *</pre>
	 * @param e
	 */
	public static void error(Throwable e){
		String error = "";
		if(e.getCause() != null)
			error = e.getCause().getMessage();
		else
			error = e.getMessage();
		putLog("FAIL:"+ error);
	}
}